package com.ruoyi.web.publish.story.cotroller;

import java.net.UnknownHostException;
import java.util.List;

import com.ruoyi.web.publish.resume.domain.Resume;
import com.ruoyi.web.publish.story.service.IStoryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.publish.story.domain.Story;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 我的故事Controller
 *
 * @author houxuzhong
 * @date 2021-08-24
 */
@RestController
@RequestMapping("/publish/story")
public class StoryController extends BaseController
{
    @Autowired
    private IStoryService storyService;

    /**
     * 查询我的故事列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Story story)
    {
        startPage();
        List<Story> list = storyService.selectStoryList(story);
        return getDataTable(list);
    }

    /**
     * 导出我的故事列表
     */
    @Log(title = "我的故事", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Story story)
    {
        List<Story> list = storyService.selectStoryList(story);
        ExcelUtil<Story> util = new ExcelUtil<Story>(Story.class);
        return util.exportExcel(list, "我的故事数据");
    }

    /**
     * 获取我的故事详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(storyService.selectStoryById(id));
    }

    /**
     * 获取我的故事详细信息
     */
    @GetMapping(value = "/getMyStory")
    public AjaxResult getMyStory() {
        return AjaxResult.success(storyService.getMyStory());
    }

    /**
     * 根据id获取我的故事
     */
    @GetMapping(value = "/getUserStory/{id}")
    public  AjaxResult getUserStory(@PathVariable("id") Long id) {
        return AjaxResult.success(storyService.getUserStory(id));
    }

    /**
     * 新增我的故事
     */
    @Log(title = "我的故事", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Story story) throws UnknownHostException {
        return toAjax(storyService.insertStory(story));
    }

    /**
     * 保存我的故事
     */
    @Log(title = "我的故事", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody Story story) throws UnknownHostException {
        if (null == story || null == story.getId()) {
            return toAjax(storyService.insertStory(story));
        } else {
            return toAjax(storyService.updateStory(story));
        }
    }

    /**
     * 修改我的故事
     */
    @Log(title = "我的故事", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Story story) throws UnknownHostException {
        return toAjax(storyService.updateStory(story));
    }

    /**
     * 删除我的故事
     */
    @Log(title = "我的故事", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(storyService.deleteStoryByIds(ids));
    }
}
